package com.lancoo.answer.widget

import android.content.Context
import android.graphics.*
import android.view.View
import com.lancoo.answer.util.CorrectMistakeUtils
import com.lancoo.answer.util.DensityUtils

/**
 * Created by wealive on 2021/9/9.
 * 改错题绘制相关
 *
 */
class DrawView : View {
    private var view: View? = null
    private var list = listOf<View>()
    private var text: String = ""
    private var textAnswer: String = ""
    private var type: Int = 0
    private var color: Int = 0
    private var isFirst: Boolean = false
    private var isAnalysis: Boolean = false
    private var startX = 0f
    private var height = 0f
    private val maxX = DensityUtils.getScreenWidth(context)

    constructor(
        isAnalysis: Boolean,
        isFirst: Boolean,
        view: View,
        text: String,
        textAnswer: String,
        type: Int,
        color: Int,
        context: Context
    ) : super(context) {
        this.isAnalysis = isAnalysis
        this.isFirst = isFirst
        this.view = view
        this.text = text
        this.textAnswer = textAnswer
        this.type = type
        this.color = color
        this.paint = Paint()
        this.path = Path()
    }

    constructor(
        isAnalysis: Boolean,
        isFirst: Boolean,
        text: String,
        textAnswer: String,
        list: List<View>,
        type: Int,
        color: Int,
        context: Context
    ) : super(context) {
        this.isAnalysis = isAnalysis
        this.isFirst = isFirst
        this.text = text
        this.textAnswer = textAnswer
        this.list = list
        this.type = type
        this.color = color
        this.paint = Paint()
        this.path = Path()
    }

    private val paint: Paint
    private val path: Path
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        paint.style = Paint.Style.STROKE
        paint.color = Color.parseColor(
            when (color) {
                0 -> "#0ec06a"
                1 -> "#0099ff"
                else -> "#ff4d4d"
            }
        )
        paint.isAntiAlias = true
        when (type) {
            -1 -> {
                paint.style = Paint.Style.FILL
                paint.typeface = Typeface.createFromAsset(context.assets, "fonts/TimesNew.ttf")
                paint.textSize = DensityUtils.dp2px(context, 7f).toFloat()
                canvas?.drawText(
                    text,
                    view!!.x - paint.measureText(text) + DensityUtils.dp2px(context, 28f),
                    view!!.y + if (isAnalysis) DensityUtils.dp2px(
                        context,
                        26f
                    ) else DensityUtils.dp2px(context, 20f),
                    paint
                )
            }

            0 -> {
                paint.style = Paint.Style.FILL
                paint.strokeWidth = DensityUtils.dp2px(context, 2f).toFloat()
                canvas?.drawLine(
                    view!!.x + if (isFirst) DensityUtils.dp2px(
                        context,
                        32f
                    ) else DensityUtils.dp2px(context, 16f),
                    view!!.y + view!!.height / 2,
                    view!!.x + view!!.width + DensityUtils.dp2px(context, 16f),
                    view!!.y + view!!.height / 2,
                    paint
                )
                if (text.isNotEmpty()) {
                    paint.color = Color.parseColor("#ff4d4d")
                    paint.textSize = DensityUtils.dp2px(context, 11f).toFloat()
                    height = paint.fontMetrics.bottom - paint.fontMetrics.top
                    canvas?.drawText(
                        text,
                        view!!.x + (view!!.width - paint.measureText(text)) / 2 + if (isFirst) DensityUtils.dp2px(
                            context,
                            32f
                        ) else DensityUtils.dp2px(context, 16f),
                        view!!.y + view!!.height + height - DensityUtils.dp2px(context, 18f),
                        paint
                    )
                }
            }

            1 -> {
                paint.style = Paint.Style.FILL
                paint.strokeWidth = DensityUtils.dp2px(context, 2f).toFloat()
                startX =
                    view!!.x + if (isFirst) DensityUtils.dp2px(
                        context,
                        32f
                    ) else DensityUtils.dp2px(context, 16f)
                canvas?.drawLine(
                    startX,
                    view!!.y + view!!.height - if (isAnalysis) DensityUtils.dp2px(
                        context,
                        16f
                    ) else DensityUtils.dp2px(
                        context, 10f
                    ),
                    view!!.x + view!!.width + DensityUtils.dp2px(context, 16f),
                    view!!.y + view!!.height - if (isAnalysis) DensityUtils.dp2px(
                        context,
                        16f
                    ) else DensityUtils.dp2px(
                        context, 10f
                    ),
                    paint
                )
                if (CorrectMistakeUtils.isContainChinese(text)) {
                    paint.typeface = Typeface.DEFAULT
                    paint.textSize = DensityUtils.dp2px(context, 11f).toFloat()
                } else {
                    paint.typeface =
                        Typeface.createFromAsset(context.assets, "fonts/TimesNew.ttf")
                    paint.textSize = DensityUtils.dp2px(context, 14f).toFloat()
                }
                height = paint.fontMetrics.bottom - paint.fontMetrics.top
                canvas?.drawText(
                    text,
                    (startX + (view!!.width - paint.measureText(text)) / 2 - if (isFirst) DensityUtils.dp2px(
                        context, 8f
                    ) else 0).let {
                        when {
                            it <= 0 -> DensityUtils.dp2px(context, 5f).toFloat()
                            it + paint.measureText(text) >= maxX -> maxX - DensityUtils.dp2px(
                                context,
                                5f
                            ) - paint.measureText(
                                text
                            )

                            else -> it
                        }
                    },
                    view!!.y + view!!.height + height - if (isAnalysis) DensityUtils.dp2px(
                        context,
                        18f
                    ) else DensityUtils.dp2px(
                        context, 10f
                    ),
                    paint
                )
                if (textAnswer.isNotEmpty()) {
                    paint.color = Color.parseColor("#0ec06a")
                    paint.typeface = Typeface.createFromAsset(context.assets, "fonts/TimesNew.ttf")
                    paint.textSize = DensityUtils.dp2px(context, 14f).toFloat()
                    canvas?.drawText(
                        textAnswer,
                        (startX + (view!!.width - paint.measureText(textAnswer)) / 2 - if (isFirst) DensityUtils.dp2px(
                            context, 8f
                        ) else 0).let {
                            when {
                                it <= 0 -> DensityUtils.dp2px(context, 5f).toFloat()
                                it + paint.measureText(textAnswer) >= maxX -> maxX - DensityUtils.dp2px(
                                    context, 5f
                                ) - paint.measureText(
                                    textAnswer
                                )

                                else -> it
                            }
                        },
                        view!!.y + view!!.height + height * 2 - DensityUtils.dp2px(
                            context,
                            18f
                        ),
                        paint
                    )
                }
            }

            2, 3 -> {
                paint.style = Paint.Style.STROKE
                paint.strokeCap = Paint.Cap.ROUND
                paint.strokeJoin = Paint.Join.ROUND
                paint.strokeWidth = DensityUtils.dp2px(context, 2f).toFloat()
                startX =
                    if (type == 2) {
                        view!!.x + if (isFirst) DensityUtils.dp2px(
                            context,
                            32f
                        ) else DensityUtils.dp2px(context, 16f)
                    } else {
                        view!!.x + view!!.width + DensityUtils.dp2px(context, 16f)
                    }
                path.moveTo(
                    startX - DensityUtils.dp2px(context, 6f),
                    view!!.y + view!!.height - if (isAnalysis) DensityUtils.dp2px(
                        context,
                        10f
                    ) else DensityUtils.dp2px(
                        context, 4f
                    )
                )
                path.lineTo(
                    startX,
                    view!!.y + view!!.height - if (isAnalysis) DensityUtils.dp2px(
                        context,
                        16f
                    ) else DensityUtils.dp2px(
                        context, 10f
                    )
                )
                path.lineTo(
                    startX + DensityUtils.dp2px(context, 6f),
                    view!!.y + view!!.height - if (isAnalysis) DensityUtils.dp2px(
                        context,
                        10f
                    ) else DensityUtils.dp2px(
                        context, 4f
                    )
                )
                canvas?.drawPath(path, paint)
                paint.style = Paint.Style.FILL
                if (CorrectMistakeUtils.isContainChinese(text)) {
                    paint.typeface = Typeface.DEFAULT
                    paint.textSize = DensityUtils.dp2px(context, 11f).toFloat()
                } else {
                    paint.typeface =
                        Typeface.createFromAsset(context.assets, "fonts/TimesNew.ttf")
                    paint.textSize = DensityUtils.dp2px(context, 14f).toFloat()
                }
                height = paint.fontMetrics.bottom - paint.fontMetrics.top
                canvas?.drawText(
                    text,
                    (startX - paint.measureText(text) / 2).let {
                        when {
                            it <= 0 -> DensityUtils.dp2px(context, 5f).toFloat()
                            it + paint.measureText(text) >= maxX -> maxX - DensityUtils.dp2px(
                                context,
                                5f
                            ) - paint.measureText(
                                text
                            )

                            else -> it
                        }
                    },
                    view!!.y + view!!.height + height - if (isAnalysis) DensityUtils.dp2px(
                        context,
                        12f
                    ) else DensityUtils.dp2px(
                        context, 6f
                    ),
                    paint
                )
                if (textAnswer.isNotEmpty()) {
                    paint.color = Color.parseColor("#0ec06a")
                    paint.typeface = Typeface.createFromAsset(context.assets, "fonts/TimesNew.ttf")
                    paint.textSize = DensityUtils.dp2px(context, 14f).toFloat()
                    canvas?.drawText(
                        textAnswer,
                        (startX - paint.measureText(textAnswer) / 2).let {
                            when {
                                it <= 0 -> DensityUtils.dp2px(context, 5f).toFloat()
                                it + paint.measureText(textAnswer) >= maxX -> maxX - DensityUtils.dp2px(
                                    context, 5f
                                ) - paint.measureText(
                                    textAnswer
                                )

                                else -> it
                            }
                        },
                        view!!.y + view!!.height + height * 2 - if (isAnalysis) DensityUtils.dp2px(
                            context, 12f
                        ) else DensityUtils.dp2px(context, 6f),
                        paint
                    )
                }
            }

            4 -> {
                paint.style = Paint.Style.STROKE
                paint.strokeWidth = DensityUtils.dp2px(context, 2f).toFloat()
                if (list.size > 1) {
                    list.forEachIndexed { index, child ->
                        if (index == 0) {
                            if (list[1].y != child.y) {
                                path.moveTo(
                                    child.x + DensityUtils.dp2px(context, 32f),
                                    child.y + child.height - if (isAnalysis) DensityUtils.dp2px(
                                        context,
                                        16f
                                    ) else DensityUtils.dp2px(
                                        context, 10f
                                    )
                                )
                                path.lineTo(
                                    child.x + child.width + DensityUtils.dp2px(context, 16f),
                                    child.y + child.height - if (isAnalysis) DensityUtils.dp2px(
                                        context,
                                        16f
                                    ) else DensityUtils.dp2px(
                                        context, 10f
                                    )
                                )
                            } else if (list[1].y == child.y) {
                                path.moveTo(
                                    child.x + DensityUtils.dp2px(context, 32f),
                                    child.y + child.height - if (isAnalysis) DensityUtils.dp2px(
                                        context,
                                        16f
                                    ) else DensityUtils.dp2px(
                                        context, 10f
                                    )
                                )
                            }
                        } else if (index < list.size - 1) {
                            if (list[index - 1].y == child.y && list[index + 1].y != child.y) {
                                path.lineTo(
                                    child.x + child.width + DensityUtils.dp2px(context, 16f),
                                    child.y + child.height - if (isAnalysis) DensityUtils.dp2px(
                                        context,
                                        16f
                                    ) else DensityUtils.dp2px(
                                        context, 10f
                                    )
                                )
                            } else if (list[index - 1].y != child.y && list[index + 1].y == child.y) {
                                path.moveTo(
                                    child.x + DensityUtils.dp2px(context, 16f),
                                    child.y + child.height - if (isAnalysis) DensityUtils.dp2px(
                                        context,
                                        16f
                                    ) else DensityUtils.dp2px(
                                        context, 10f
                                    )
                                )
                            }
                        } else {
                            if (list[index - 1].y == child.y) {
                                path.lineTo(
                                    child.x + child.width + DensityUtils.dp2px(context, 16f),
                                    child.y + child.height - if (isAnalysis) DensityUtils.dp2px(
                                        context,
                                        16f
                                    ) else DensityUtils.dp2px(
                                        context, 10f
                                    )
                                )
                            } else if (list[index - 1].y != child.y) {
                                path.moveTo(
                                    child.x + DensityUtils.dp2px(context, 16f),
                                    child.y + child.height - if (isAnalysis) DensityUtils.dp2px(
                                        context,
                                        16f
                                    ) else DensityUtils.dp2px(
                                        context, 10f
                                    )
                                )
                                path.lineTo(
                                    child.x + child.width + DensityUtils.dp2px(context, 16f),
                                    child.y + child.height - if (isAnalysis) DensityUtils.dp2px(
                                        context,
                                        16f
                                    ) else DensityUtils.dp2px(
                                        context, 10f
                                    )
                                )
                            }
                        }
                        canvas?.drawPath(path, paint)
                    }
                } else {
                    canvas?.drawLine(
                        list[0].x + if (isFirst) DensityUtils.dp2px(
                            context,
                            32f
                        ) else DensityUtils.dp2px(context, 16f),
                        list[0].y + list[0].height - if (isAnalysis) DensityUtils.dp2px(
                            context,
                            16f
                        ) else DensityUtils.dp2px(context, 10f),
                        list[0].x + list[0].width + DensityUtils.dp2px(context, 16f),
                        list[0].y + list[0].height - if (isAnalysis) DensityUtils.dp2px(
                            context,
                            16f
                        ) else DensityUtils.dp2px(context, 10f),
                        paint
                    )
                }
                paint.style = Paint.Style.FILL
                if (CorrectMistakeUtils.isContainChinese(text)) {
                    paint.typeface = Typeface.DEFAULT
                    paint.textSize = DensityUtils.dp2px(context, 11f).toFloat()
                } else {
                    paint.typeface =
                        Typeface.createFromAsset(context.assets, "fonts/TimesNew.ttf")
                    paint.textSize = DensityUtils.dp2px(context, 14f).toFloat()
                }
                startX = list[0].x + if (isFirst) DensityUtils.dp2px(
                    context,
                    32f
                ) else DensityUtils.dp2px(context, 16f)
                height = paint.fontMetrics.bottom - paint.fontMetrics.top
                if (list.size > 1) {
                    canvas?.drawText(
                        text,
                        (list[list.size / 2].x - paint.measureText(text) / 2).let {
                            when {
                                it <= 0 -> DensityUtils.dp2px(context, 16f).toFloat()
                                it + paint.measureText(text) >= maxX -> maxX - DensityUtils.dp2px(
                                    context, 16f
                                ) - paint.measureText(
                                    text
                                )

                                else -> it
                            }
                        },
                        list[list.size / 2].y + list[list.size / 2].height + height - if (isAnalysis) DensityUtils.dp2px(
                            context, 18f
                        ) else DensityUtils.dp2px(
                            context, 12f
                        ),
                        paint
                    )
                } else {
                    height = paint.fontMetrics.bottom - paint.fontMetrics.top
                    canvas?.drawText(
                        text,
                        (startX + (list[0].width - paint.measureText(text)) / 2 - if (isFirst) DensityUtils.dp2px(
                            context, 8f
                        ) else 0).let {
                            when {
                                it <= 0 -> DensityUtils.dp2px(context, 5f).toFloat()
                                it + paint.measureText(text) >= maxX -> maxX - DensityUtils.dp2px(
                                    context,
                                    5f
                                ) - paint.measureText(
                                    text
                                )

                                else -> it
                            }
                        },
                        list[0].y + list[0].height + height - if (isAnalysis) DensityUtils.dp2px(
                            context,
                            18f
                        ) else DensityUtils.dp2px(
                            context, 10f
                        ),
                        paint
                    )
                }
                if (textAnswer.isNotEmpty()) {
                    paint.color = Color.parseColor("#0ec06a")
                    if (CorrectMistakeUtils.isContainChinese(textAnswer)) {
                        paint.typeface = Typeface.DEFAULT
                        paint.textSize = DensityUtils.dp2px(context, 11f).toFloat()
                    } else {
                        paint.typeface =
                            Typeface.createFromAsset(context.assets, "fonts/TimesNew.ttf")
                        paint.textSize = DensityUtils.dp2px(context, 14f).toFloat()
                    }
                    canvas?.drawText(
                        textAnswer,
                        (list[list.size / 2].x - paint.measureText(textAnswer) / 2).let {
                            when {
                                it <= 0 -> DensityUtils.dp2px(context, 16f).toFloat()
                                it + paint.measureText(textAnswer) >= maxX -> maxX - DensityUtils.dp2px(
                                    context, 16f
                                ) - paint.measureText(
                                    textAnswer
                                )

                                else -> it
                            }
                        },
                        list[list.size / 2].y + list[list.size / 2].height + 2 * height - if (isAnalysis) DensityUtils.dp2px(
                            context, 18f
                        ) else DensityUtils.dp2px(
                            context, 12f
                        ),
                        paint
                    )
                }
            }
        }
    }
}